package com.xb.loan.cashier.biz.service.impl;

import com.xb.loan.cashier.biz.dao.CashierPayChannelDao;
import com.xb.loan.cashier.biz.dao.bean.CashierPayChannel;
import com.xb.loan.cashier.biz.service.CacheService;
import com.xb.loan.cashier.biz.service.CashierPayChannelService;
import com.xb.loan.cashier.biz.service.CashierPlatPaytypeService;
import com.xb.loan.cashier.biz.service.cache.RedisMethodCacheSupport;
import com.xb.loan.cashier.service.constant.ICashierEnumConst;
import com.xb.loan.cashier.service.exception.ICashierServiceException;
import com.xb.loan.log.exception.BizException;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 第三方支付渠道
 */
@Service("cashierPayChannelService")
public class CashierPayChannelServiceImpl implements CashierPayChannelService, CacheService {

    @Autowired
    private CashierPayChannelDao cashierPayChannelDao;
    @Autowired
    private CashierPlatPaytypeService cashierPlatPaytypeService;
    
    
    @Override
    public List<CashierPayChannel> queryListByPlatId(String platId, int payType) {
        List<CashierPayChannel> list = RedisMethodCacheSupport.getBeans(CashierPayChannel.class, platId);
        if(CollectionUtils.isNotEmpty(list)){
            return list;
        }
        List<String> paychannelIds = cashierPlatPaytypeService.queryByPlatId(platId);
        if (CollectionUtils.isNotEmpty(paychannelIds)) {
            list = new ArrayList<CashierPayChannel>();
            for(String channelId : paychannelIds){
            	CashierPayChannel cashierPayChannel = cashierPayChannelDao.queryByChannelId(channelId);
            	if (cashierPayChannel!=null && cashierPayChannel.getPayType()==payType) {
            		list.add(cashierPayChannel);
            	}
            }
        }
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        RedisMethodCacheSupport.setBeans(CashierPayChannel.class, list, platId);
        return list;
    }

    @Override
    public CashierPayChannel queryByChannelId(String payChannelId) {
        String[] paramsCache = new String[]{payChannelId};
        CashierPayChannel channel = RedisMethodCacheSupport.getBean(CashierPayChannel.class, paramsCache);
        if(channel!=null){
            return channel;
        }
        channel = cashierPayChannelDao.queryByChannelId(payChannelId);
        if(channel==null){
            return null;
        }
        RedisMethodCacheSupport.setBean(CashierPayChannel.class, channel, paramsCache);
        return channel;
    }

    @Override
    public CashierPayChannel selectByPayChannelId(String payChannelId) {
        CashierPayChannel channel = RedisMethodCacheSupport.getBean(CashierPayChannel.class, payChannelId);
        if(channel!=null){
            return channel;
        }
        channel = cashierPayChannelDao.selectByPrimaryKey(payChannelId);
        if(channel==null){
            return null;
        }
        RedisMethodCacheSupport.setBean(CashierPayChannel.class, channel, payChannelId);
        return channel;
    }

    @Override
    public List<CashierPayChannel> selectByPayChannelType(int type, int payType) {
        String[] keys = new String[]{type+"", payType+""};
        List<CashierPayChannel> channels = RedisMethodCacheSupport.getBeans(CashierPayChannel.class, keys);
        if(CollectionUtils.isNotEmpty(channels)){
            return channels;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type);
        params.put("payType", payType);
        channels = cashierPayChannelDao.selectByPayChannelType(params);
        if(CollectionUtils.isEmpty(channels)){
            return null;
        }
        RedisMethodCacheSupport.setBeans(CashierPayChannel.class, channels, keys);
        return channels;
    }

    @Override
    public List<CashierPayChannel> selectAll() {
        List<CashierPayChannel> channels = RedisMethodCacheSupport.getBeans(CashierPayChannel.class);
        if(CollectionUtils.isNotEmpty(channels)){
            return channels;
        }
        channels = cashierPayChannelDao.selectAll();
        if(CollectionUtils.isEmpty(channels)){
            return null;
        }
        RedisMethodCacheSupport.setBeans(CashierPayChannel.class, channels);
        return channels;
    }
    
    @Override
    public void clearCache(String methodName) {
        RedisMethodCacheSupport.clear(methodName);
    }

    @Override
    public void clearCacheAll() {
        RedisMethodCacheSupport.clearAll();
    }

    @Override
    public List<CashierPayChannel> selectByPayType(int payType) {
        String[] keys = new String[]{payType+""};
        List<CashierPayChannel> channels = RedisMethodCacheSupport.getBeans(CashierPayChannel.class, keys);
        if(CollectionUtils.isNotEmpty(channels)){
            return channels;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("payType", payType);
        channels = cashierPayChannelDao.selectByPayType(params);
        if(CollectionUtils.isEmpty(channels)){
            return null;
        }
        RedisMethodCacheSupport.setBeans(CashierPayChannel.class, channels, keys);
        return channels;
    }

	@Override
	public void savePayChannel(CashierPayChannel payChannel) throws BizException {
		CashierPayChannel cpc = cashierPayChannelDao.queryByPayChannelName(payChannel.getPayChannelName());
		if (cpc!=null) {
			throw ICashierServiceException.GATEWAY_NAME_EXIST;
		}
		String payChannelId = FormatOperation.assemblySeq(
				ICashierEnumConst.CashierPriKeyStrategy.CASHIER_PAYCHANNEL.getCode(),
				ICashierEnumConst.CashierPriKeyStrategy.CASHIER_PAYCHANNEL.getNum(),
				FormatOperation.formatDateToYMDHms(new Date()), "");
		payChannel.setPayChannelId(payChannelId);
		int res = cashierPayChannelDao.insertSelective(payChannel);
		if (res<1) {
			throw ICashierServiceException.systemException;
		}
	}

	@Override
	public void delPayChannel(String payChannelId) throws BizException{
		int res = cashierPayChannelDao.deleteByPrimaryKey(payChannelId);
		if (res<1) {
			throw ICashierServiceException.systemException;
		}
	}

	@Override
	public void updatePayChannel(CashierPayChannel payChannel) throws BizException{
		CashierPayChannel cpc = cashierPayChannelDao.selectByPrimaryKey(payChannel.getPayChannelId());
		if (cpc!=null && !cpc.getPayChannelName().equals(payChannel.getPayChannelName())) {
			cpc = cashierPayChannelDao.queryByPayChannelName(payChannel.getPayChannelName());
			if (cpc!=null) {
				throw ICashierServiceException.GATEWAY_NAME_EXIST;
			}
		}
		int res = cashierPayChannelDao.updateByPrimaryKeySelective(payChannel);
		if (res<1) {
			throw ICashierServiceException.systemException;
		}
	}

	@Override
	public List<Map<String, Object>> queryPayChannelList() {
		return cashierPayChannelDao.queryPayChannelList();
	}

	@Override
	public List<CashierPayChannel> queryPayGateWayById(String platId) {
		return cashierPayChannelDao.queryPayGateWayById(platId);
	}

}
